package main

import (
	"fmt"
	"math/rand"
	"time"
)

func asyncTask(id int) string {
	time.Sleep(time.Millisecond * time.Duration(
		rand.Intn(2000)))
	return fmt.Sprintf("task-%d", id)
}

func doWork() string {
	numOfRunner := 10
	ch := make(chan string, numOfRunner)
	for i := 0; i < numOfRunner; i++ {
		go func(id int) {
			result := asyncTask(id)
			// 为了避免goroutine泄露,  chan buffer最好和任务数量一致
			ch <- result
		}(i)
	}

	return <-ch
}

func main() {
	result := doWork()
	fmt.Println(result)
}
